﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public sealed class SpriteMa : Singleton<SpriteMa>
{
    private SpriteMa()
    {
        
        Init();
    }

    private bool mInit = false;
    public Sprite[] mSpriteAry;
    private Dictionary<string, Sprite> mSpriteDic = new Dictionary<string, Sprite>();
    private Dictionary<string, Dictionary<string, Sprite>> mSpriteAtlasDic = new Dictionary<string, Dictionary<string, Sprite>>();
    private Dictionary<string, Sprite[]> mSpriteAtlasAry = new Dictionary<string, Sprite[]>();
    
    public void Init()
    {
        if (mInit)
            return;

        mInit = true;
        //Sprite[] sList = Resources.LoadAll<Sprite>("Atlas/Number1");
        //AddSpriteRange("Number1", sList);



        if (mSpriteAry != null)
        {
            for (int i = 0; i < mSpriteAry.Length; i++)
            {
                if (mSpriteAry[i] == null)
                    continue;

                if (mSpriteDic.ContainsKey(mSpriteAry[i].name))
                {
                    Debug.LogError("spriteManager name same" + mSpriteAry[i].name);
                    continue;
                }
                mSpriteDic.Add(mSpriteAry[i].name, mSpriteAry[i]);
            }
        }

    }

    public void AddSpriteRange(string altas, Sprite[] list)
    {
        if (mSpriteAtlasDic.ContainsKey(altas) == false)
        {
            mSpriteAtlasDic.Add(altas, new Dictionary<string, Sprite>());
        }
        var dic = mSpriteAtlasDic[altas];
        foreach (var sprite in list)
        {
            dic.Add(sprite.name, sprite);
        }

        mSpriteAtlasAry.Add(altas, list);
    }

    public Sprite GetSprite(string atlas, string name)
    {

        if (mSpriteAtlasDic.ContainsKey(atlas) == false)
            return null;

        Dictionary<string, Sprite> s = mSpriteAtlasDic[atlas];

        if (s.ContainsKey(name) == false)
        {
            PLDebug.LogError("没有找到图片:" + name);
            return null;
        }


        return s[name];
    }

    public Dictionary<string, Sprite> GetSprtieDic(string atlas)
    {
        if (mSpriteAtlasDic.ContainsKey(atlas) == false)
            return null;

        Dictionary<string, Sprite> s = mSpriteAtlasDic[atlas];
        return s;
    }

    public Sprite[] GetSprtieAry(string atlas)
    {
        if (mSpriteAtlasAry.ContainsKey(atlas) == false)
            return null;

        Sprite [] s = mSpriteAtlasAry[atlas];
        return s;
    }


    

}
